package com.demo.common.core.config;

import com.demo.common.base.exception.BaseException;
import com.demo.common.base.pojo.JsonRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;
import java.net.ConnectException;
import java.nio.file.AccessDeniedException;
import java.security.SignatureException;

@Slf4j
@Order(9999)
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 系统异常处理器
     * @param throwable 议程
     * @param request 请求
     * @return Void
     */
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)   // 指定http状态码，如果指定为2xx，feign不会抛出异常
    public JsonRes<Void> systemExceptionHandler(Throwable throwable, HttpServletRequest request) {
        log.error("URL：{} ,系统异常", request.getRequestURI(), throwable);
        return JsonRes.errorMsg(throwable.getMessage());
    }

    /**
     * 自定义异常处理器
     */
    @ExceptionHandler(BaseException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)   // 指定http状态码，如果指定为2xx，feign不会抛出异常
    public JsonRes<Void> baseExceptionHandler(BaseException baseException, HttpServletRequest request) {
        log.warn("URL：{} ,业务异常", request.getRequestURI(),baseException);
        return JsonRes.errorMsg(baseException.getMessage());
    }



    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return JsonRes.error(404, "路径不存在，请检查路径是否正确");
    }
    @ExceptionHandler(AuthException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> usernameIsExitedException(AuthException e){
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("用户已经存在");
    }


    @ExceptionHandler(ConnectException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> connectException(ConnectException e){
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("系统调用异常");
    }

    @ExceptionHandler(ResourceAccessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> connectException(ResourceAccessException e){
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("系统之间调用异常");
    }


    @ExceptionHandler(value = SignatureException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> signatureException(SignatureException e) {
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("Token格式错误");
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> illegalArgumentException(IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("Token非法参数异常");
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonRes<Object> accessDeniedException(AccessDeniedException e) {
        log.error(e.getMessage(), e);
        return JsonRes.errorMsg("Token非法参数异常");
    }

}
